package com.shengyin.ec.model.dao.cluster;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.shengyin.ec.model.dao.BaseDaoImpl;
import com.shengyin.ec.model.entity.ChainCompany;
import com.shengyin.ec.model.entity.ClusterCompany;
import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.Goods;
import com.shengyin.ec.model.entity.Orders;

@Repository("clusterDao")
public class ClusterDaoImpl extends BaseDaoImpl<ClusterCompany> implements ClusterDao {

	@Override
	public void createClusterCompany(ClusterCompany clusterCompany) {
		super.save(clusterCompany);
	}

	@Override
	public void deleteClusterCompany(ClusterCompany clusterCompany) {
		ClusterCompany clusterCompany1 = super.get(clusterCompany.getId());
		super.delete(clusterCompany1);
	}

	@Override
	public void deleteClusterCompany(long id) {
		super.deleteByKey(id);
		
	}

	@Override
	public void modifyClusterCompany(ClusterCompany clusterCompany) {
		//ClusterCompany clusterCompany1 = super.get(clusterCompany.getId());	
		super.update(clusterCompany);
	}

	@Override
	public ClusterCompany getClusterCompany(ClusterCompany clusterCompany) {
		return super.get(clusterCompany.getId());
		}

	@Override
	public ClusterCompany getClusterCompany(long id) {
		return super.get(id);
	}

	@Override
	public List<ClusterCompany> getClusterCompanyList() {
		super.loadAll();
		return null;
	}

	@Override
	public List<ClusterCompany> getClusterCompanyList(int start, int end) {
		return null;
	}

	@Override
	public List<ClusterCompany> getClusterCompanyListByKeyword(String keyword,
			int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}
    /*
     * 连锁管理
     */
	@Override
	public List<ChainCompany> getChainCompanyList(String keyword, int start,
			int end) {
		return null;
	}

	@Override
	public List<ChainCompany> getChainCompanyList(int start, int end) {
		return null;
	}
	

/*
 * 商品管理相关操作
 * 
 */
	@Override
	public Long getTotal(long clusterCompanyId) {
		 return (Long) super.find("select count(*) from Goods where companyId = '"+clusterCompanyId+"'").get(0);
	}

	@Override
	public void deleteSelectedCluster(String[] ids) {
		
	}
	@Override
	public ClusterCompany getClusterCompanyByUsername(String clusterCompanyUsrname){
		@SuppressWarnings("unchecked")
		List<ClusterCompany> list=super.find("from ClusterCompany cc where cc.username='"+clusterCompanyUsrname+"'");
		if(list.size()!=0)
			return list.get(0);
		return null;
	}
	
	@Override
	public List<Goods> getClusterCompanyGoodsList(int start, int end) {
		return super.findByPage("From Goods good",start, end);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Goods> getClusterCompanyGoodsList(String[] paramValues, int searchType,int offset,int pageSize) {
		
		switch(searchType){
		case 1:
			return super.findByPage("from Goods where companyId=? and productName like ?", paramValues,offset,pageSize);
		case 2:
			return super.findByPage("from Goods where companyId=? and productType like ?", paramValues,offset,pageSize);
		case 3:
			System.out.println("case ====== 3");
			return super.findByPage("from Goods where companyId=? and str(id) like ?", paramValues,offset,pageSize);
		}
		return null;
	}
	
	@Override
	public List<Goods> getClusterCompanyGoodsList(String[] paramValues) {
		
		return super.find("from Goods where companyId=? and productName like ?",paramValues);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Goods> getClusterCompanyGoodsList(
			long clusterCompanyid, int offset, int pageSize) {
		
		return super.findByPage("from Goods where companyId = '"+clusterCompanyid+"'", offset, pageSize);
		
	}
	
	public List<Goods> getClusterCompanyGoodsList(
			long clusterCompanyid,String keyword, int offset, int pageSize) {
		
		return super.findByPage("from Goods where companyId = '"+clusterCompanyid+"'", offset, pageSize);
		
		
	}
/*
 * (non-Javadoc)
 * @see com.shengyin.ec.model.dao.cluster.ClusterDao#checkCompanyUser(java.lang.String, java.lang.String)
 * 检查用户
 */
	@Override
	public boolean checkCompanyUser(String username, String password) {
		@SuppressWarnings("unchecked")
		List<ClusterCompany> list=super.find("from ClusterCompany cc where cc.username='"+username+"' and cc.pwd='"+password+"'");
		if(list.size()!=0)
			return true;
		return false;
	}
	
  /*
   * 圣茵后台集群管理
  */
	
    public List<ClusterCompany> getCompanyList(int offset,int pageSize){
    	
    	return super.findByPage("from ClusterCompany where companyType = '2'", offset, pageSize);
    }
	
	public Long getTotal(){
		
		return (Long)super.find("select count(*) from ClusterCompany where companyType='2'").get(0);
	}

    public List<ClusterCompany> getCompany(int status,int offset,int pageSize){
    	
    	return super.findByPage("from ClusterCompany where companyType='2' and status=?", status, offset, pageSize);
    }
	
	public List<ClusterCompany> getCompany(int status){
		
		return super.find("from ClusterCompany where companyType='2' and status='"+status+"'");
	}
	
    public List<ClusterCompany> getCompanyListByName(String keyword,int offset,int pageSize){
    	
    	return super.findByPage("from ClusterCompany where companyType='2' and companyname like ?", keyword, offset, pageSize);
    }
	
	public List<ClusterCompany> getCompanyListByName(String keyword){
		
		String[] values=new String[2];
		values[0]="2";
		values[1]=keyword;
		return super.find("from ClusterCompany where companyType=? and companyname like ?", values);
	}
	
}
